package net.alan.modules.repository;

import net.alan.modules.bean.Chat;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/22
 */
public interface ChatRepository extends JpaRepository<Chat, Integer> {

    List<Chat> findByProjectIdAndUserId(Integer projectId, Integer userId);

    List<Chat> findByUserIdAndReadedTrue(Integer userId);

    Chat findByProjectIdAndUserIdAndToUserId(Integer projectId, Integer userId, Integer toUserId);

    @Modifying
    @Transactional
    @Query(value = "delete from chat where project_id = ?1 and (user_id = ?2 or to_user_id = ?2)", nativeQuery = true)
    void deleteByProjectIdAndUserId(Integer projectId, Integer userId);

    /**
     * 获取自己的
     *//*
    @Query(value = "select * from chat where (to_user_id=?1 and to_read=1) or (user_id=?1 and from_read=1)", nativeQuery = true)
    List<Chat> listUnreadChat(Integer userId);*/

    /*@Query(value = "select * from chat where (to_user_id=?1 or user_id=1) or project_id=?2", nativeQuery = true)
    Chat getOne(Integer userId, Integer projectId);*/
}
